package com.xilihui.xlh.business.activitys;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.xilihui.xlh.R;
import com.xilihui.xlh.business.adapters.StoresAdapter;
import com.xilihui.xlh.business.entities.AgentsEntity;
import com.xilihui.xlh.business.entities.AreaEntity;
import com.xilihui.xlh.business.entities.ShopEntity;
import com.xilihui.xlh.business.requests.AppointmentRequest;
import com.xilihui.xlh.business.widget.MorePopWindow;
import com.xilihui.xlh.component.recyclerview.BaseAdapter;
import com.xilihui.xlh.core.app.BaseCompatActivity;
import com.xilihui.xlh.core.exception.DefaultExceptionListener;
import com.xilihui.xlh.core.exception.ExceptionManager;
import com.xilihui.xlh.core.http.DoTransform;
import com.xilihui.xlh.core.http.HttpSubscriber;
import com.xilihui.xlh.core.util.LogUtil;
import com.xilihui.xlh.core.util.SPUtil;
import com.xilihui.xlh.core.util.ToastUtil;
import com.xilihui.xlh.core.util.YEventBuses;

import org.greenrobot.eventbus.Subscribe;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/5/25.
 */

public class AppointmentStoresActivity extends BaseCompatActivity implements OnLoadMoreListener, OnRefreshListener, View.OnClickListener {
    
    @BindView(R.id.tv_toolbar_right)
    TextView tv_right;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.recyclerview)
    XRecyclerView recyclerView;
    
    ExceptionManager exceptionManager;
    StoresAdapter storesAdapter;
    MorePopWindow morePopWindow;

    ArrayList<AgentsEntity> datas = new ArrayList<>();
    BaseAdapter<AgentsEntity> baseAdapter;
    int page = 1;
    public String keyword = "";
    public String order = "default";
    
    public String lat = "";
    public String lng = "";
    
    
    @OnClick(R.id.iv_toolbar_left)
    public void back(){
        finish();
    }
    
    @OnClick(R.id.tv_toolbar_title)
    public void search(){
        startActivity(new Intent(this,SearchShopActivity.class));
    }
    
    @OnClick(R.id.tv_toolbar_right)
    public void typeSelect(){
        morePopWindow.show(tv_right);
    }
    
    @Override
    public int getLayout() {
        return R.layout.activity_appointment_stores;
    }

    @Override
    public String initTitle() {
        return null;
    }

    @Override
    public void initUI(@Nullable Bundle savedInstanceState) {

        exceptionManager = new ExceptionManager(smartRefreshLayout,new DefaultExceptionListener(this)){
            @Override
            protected int getEmptyView() {
                return R.layout.empty_shop;
            }
        };
        
        exceptionManager.showEmpty();
        
        
        smartRefreshLayout.setOnLoadMoreListener(this);
        smartRefreshLayout.setOnRefreshListener(this);
        
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setLoadingMoreEnabled(false);
        storesAdapter = new StoresAdapter(this);
        recyclerView.setAdapter(storesAdapter);
        
    }

    @Override
    public void initData() {
        morePopWindow = new MorePopWindow(this);
        lat = (String) SPUtil.get(this,SPUtil.LAT,"");
        lng = (String) SPUtil.get(this,SPUtil.LNG,""); 
        getData(true);
    }
    
    
    public void getData(boolean isLoading){
        
        AppointmentRequest.shoplist(this,lng,lat,page,keyword,order)
                .compose(DoTransform.<ShopEntity>applyScheduler(this,isLoading))
                .compose(AppointmentStoresActivity.this.<ShopEntity>bindToLifecycle())
                .subscribe(new HttpSubscriber<ShopEntity>(this) {
                    @Override
                    protected void onSuccess(ShopEntity entity) {
                        
                        if(page==1){
                            datas.clear();
                        }
                        
                        if(order.equals("default")){
                            ArrayList<AgentsEntity> oftens = (ArrayList<AgentsEntity>) entity.getData().getOften();
                            if(oftens!=null&&oftens.size()>0){
                                LogUtil.i("mylog",">0");
                                AgentsEntity agentBean = new AgentsEntity();
                                agentBean.setShowType(3);
                                datas.add(agentBean);
                                datas.addAll(oftens);
                                AgentsEntity agentBean1 = new AgentsEntity();
                                agentBean1.setShowType(2);
                                datas.add(agentBean1);
                                
                                
                                morePopWindow.showTv();
                            }
                            LogUtil.i("mylog","order==");
                        }else {
                            morePopWindow.dismissTv();
                        }
                        
                        datas.addAll(entity.getData().getAgent());
                        storesAdapter.setData(datas);
                        
                        if(datas.size()>0){
                            exceptionManager.hide();
                        }else {
                            exceptionManager.showEmpty();
                        }
                        
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                        
                        if(page>=entity.getPageCount()){
                            smartRefreshLayout.finishLoadMoreWithNoMoreData();
                        }else {
                            smartRefreshLayout.setNoMoreData(false);
                        }
                        
                    }

                    @Override
                    protected void onFail(String msg) {
                        ToastUtil.toastShortNegative(msg);
                        smartRefreshLayout.finishRefresh();
                        smartRefreshLayout.finishLoadMore();
                    }
                });
        
        
        
    }


    @Override
    public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
        page = page + 1;
        getData(false);
    }

    @Override
    public void onRefresh(@NonNull RefreshLayout refreshLayout) {
        page = 1;
        getData(false);
    }

    @Override
    public void onClick(View view) {
        
    }

    @Subscribe
    public void onEventMainThread(YEventBuses.Event event) {
        super.onEventMainThread(event);
        if (event.is(MorePopWindow.SHOPSIFT)) {
            page = 1; 
            order = (String) event.getParams(0);
            datas.clear();
            getData(false);
        }else if(event.is(SearchShopActivity.search)){
            page = 1;
            keyword = (String) event.getParams(0);
            order = "";
            try {
                keyword = URLDecoder.decode(keyword,"UTF-8");
                getData(true);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
       
    }
}
